package com.qiku.controller;

import com.github.pagehelper.PageInfo;
import com.qiku.domain.Resource;
import com.qiku.domain.ResourceVo;
import com.qiku.domain.ResponseResult;
import com.qiku.service.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/resource")
public class ResourceController {

    @Autowired
    private ResourceService resourceService;

    /**
     * 资源信息分页&条件查询
     * @param resourceVo
     * @return
     */
    @PostMapping("/findAllResource")
    public ResponseResult findAllResource(@RequestBody ResourceVo resourceVo){
        PageInfo<Resource> pageInfo = resourceService.findAllResource(resourceVo);
        ResponseResult result = new ResponseResult(true, 200, "响应成功~", pageInfo);
        return result;
    }

    /**
     * 添加&更新资源信息
     * @param resource
     * @return
     */
    @PostMapping("/saveOrUpdateResource")
    public ResponseResult saveOrUpdateResource(@RequestBody Resource resource){
        if (null != resource.getId() && 0 < resource.getId()){
            resourceService.updateResource(resource);
            ResponseResult result = new ResponseResult(true, 200, "修改成功~", null);
            return result;
        } else {
            resourceService.saveResource(resource);
            ResponseResult result = new ResponseResult(true, 200, "保存成功~", null);
            return result;
        }
    }

    /**
     * 删除资源信息
     * @param id
     * @return
     */
    @GetMapping("/deleteResource")
    public ResponseResult deleteResource(Integer id){
        resourceService.deleteResource(id);
        ResponseResult result = new ResponseResult(true, 200, "响应成功~", null);
        return result;
    }
}
